package com.qf.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitConfirmCallBackHandle implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {

    private static Logger logger = LoggerFactory.getLogger(RabbitConfirmCallBackHandle.class);

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void rabbitTemplate(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        logger.debug("当前的消息得标识为："+correlationData);
        if (ack){
            logger.debug(correlationData+"的消息投递到了交换机中");
        }else {
            logger.error(correlationData+"消息投递失败。失败原因：{}"+cause);
        }

    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        logger.error("进入了错误回调{}消息体为："+message);
        logger.error("进入了错误回调{}错误码："+replyCode);
        logger.error("进入了错误回调{}错误描述："+replyText);
        logger.error("进入了错误回调{}交换机："+exchange);
        logger.error("进入了错误回调{}路由键："+routingKey);

    }
}
